﻿#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: PlayersController.cs
//

#endregion

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Ids.Utils;
using Novagoal.Logic;
using Novagoal.Models;
using Novagoal.Utils;
using Novagoal.Website.Models.Forms;
using Novagoal.Website.Models.Views;
using Novagoal.Website.WebApp.Routing;

namespace Novagoal.Website.WebApp.Controllers
{
    public class PlayersController : ListController
    {
        #region PROPERTIES

        private readonly TeamsLogic _teamsLogic;
        private readonly PlayersLogic _playersLogic;

        #endregion

        #region PUBLIC METHODS

        public PlayersController(UsersLogic usersLogic, TeamsLogic teamsLogic, PlayersLogic playersLogic)
            : base(usersLogic)
        {
            _teamsLogic = teamsLogic;
            _playersLogic = playersLogic;
        }

        [HttpGet]
        public ActionResult Index()
        {
            var players = _playersLogic.GetPlayers(Request.Params, SortField, SortDirection, PageNumber, PageSize);

            var indexViewModel = new IndexViewModel
            {
                List = players,
                RouteValues = players.GetRouteValues(PageSize)
            };
            return View("Index", indexViewModel);
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return Redirect(collection);
        }

        [HttpGet]
        public ActionResult Add()
        {
            var viewModel = new PlayersViewModel
                                {
                                    Player = new Player(),
                                    PlaysIn = new Dictionary<Team, bool>(),
                                    Teams = _teamsLogic.GetTeams(),
                                };
            return View("Details", viewModel);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var player = _playersLogic.GetPlayer(id);

            var viewModel = new PlayersViewModel
            {
                Player = player,
                Position = CreatePosition(player.Position),
                PlaysIn = _teamsLogic.GetTeamsByPlayer(id),
                Teams = _teamsLogic.GetTeams(),
            };

            return View("Details", viewModel);
        }

        [HttpPost]
        public ActionResult Save(PlayerFormModel formModel)
        {
            var modelView = CreateModelView(formModel);
            if(ModelState.IsValid)
            {
                modelView.Player.Id = _playersLogic.SavePlayer(modelView.Player);
                _teamsLogic.SavePlayerInTeams(modelView.Player, formModel.TeamId, formModel.Enabled);

                modelView.PlaysIn = _teamsLogic.GetTeamsByPlayer(modelView.Player.Id);
                modelView.SuccessMessage = "Player saved successfully";
            }
            return View("Details", modelView);
        }

        #endregion

        #region PRIVATE METHODS

        private PlayersViewModel CreateModelView(PlayerFormModel formModel)
        {
            var currentUser = GetUserInformation();
            var modelView = new PlayersViewModel
                                {
                                    Player = new Player
                                                 {
                                                     Id = formModel.PlayerId,
                                                     Name = formModel.Name,
                                                     Mvp = formModel.Mvp,
                                                     UpdatedBy = currentUser.Person.Id,
                                                     Position = formModel.Position.ToStringValue(),
                                                 },
                                    Position = formModel.Position,
                                    Teams = _teamsLogic.GetTeams(),
                                    PlaysIn = _teamsLogic.GetTeamsByPlayer(formModel.PlayerId)
                                };
            return modelView;
        }

        private Enums.PlayerPositions CreatePosition(string position)
        {
            foreach (Enums.PlayerPositions playerPosition in Enum.GetValues(typeof(Enums.PlayerPositions)))
            {
                if(playerPosition.ToStringValue() == position)
                {
                    return playerPosition;
                }
            }
            return Enums.PlayerPositions.Goalkeeper;
        }

        #endregion
    }
}
